﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;

namespace XNET.SQL
{
    /// <summary>
    /// 数据库通用接口
    /// </summary>
    public interface IDatabase
    {
        /// <summary>
        /// 执行SQL语句返回影响行数
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters"> 参数</param>
        /// <returns></returns>
        int ExecuteNonQuery(string sql, object parameters = null);
        /// <summary>
        /// 执行SQL语句查询返回集合
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="action">返回事件</param>
        /// <returns></returns>
        IEnumerable<T> ExecuteDataReader<T>(string sql, object parameters, Func<DbDataReader, T> action);
        /// <summary>
        /// 执行SQL语句查询返回集合
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="action">返回集合事件</param>
        void ExecuteDataReader(string sql, object parameters, Action<DbDataReader> action);
        /// <summary>
        /// 执行事务
        /// </summary>
        /// <param name="action">在事件中所有查询，修改，插入操作都在一个事务</param>
        void ExecuteTransaction(Action<IDatabase> action);
        /// <summary>
        /// 返回单个值
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        T ExecuteScalar<T>(string sql,object parameters = null);
        /// <summary>
        /// 批量复制
        /// </summary>
        /// <param name="table">数据表</param>
        /// <param name="batchSize">每次复制条数</param>
        void BulkCopy(DataTable table, int batchSize = 100);
        /// <summary>
        /// 判断是否存在行
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        bool HasRow(string sql, object parameters = null);
    }
}
